package com.epay.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.modular.system.entity.Parms;
import cn.stylefeng.guns.modular.system.entity.PlatChannelReport;
import com.alibaba.fastjson.JSONObject;
import com.epay.Epay;
import com.epay.EpayEncrypt;
import com.epay.RespDataApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service("platChannelService")
public class PlatChannelService {
    @Resource
    RedisTemplate redisTemplate;
    private String aesEncrypt(String reqContent, String ivKey) {
        Parms parms = (Parms) redisTemplate.opsForHash().get(Epay.PARAM_KEY, Epay.BaseParams.AES_KEY_CODE);
        String aseKey = parms.getVal();
        return EpayEncrypt.phpAesEncrypt(aseKey, ivKey, reqContent);
    }


    public int updateReport(PlatChannelReport platChannelReport) {
        String iv = RandomUtil.randomNumbers(16);
        String content = aesEncrypt(JSONObject.toJSONString(platChannelReport),iv);
        Parms parms = (Parms) redisTemplate.opsForHash().get(Epay.PARAM_KEY, Epay.BaseParams.OPS_RECHARGE_API);
        String form = HttpUtil.createPost(parms.getVal() +"/platChannel/updateReport.api?reqParam="+content+"&iv="+iv).execute().body();
        log.error(" update rechrage updateReport response {} {}",form,JSONObject.toJSONString(platChannelReport));

//        RespDataApi respDataApi = JSONObject.parseObject(form ,RespDataApi.class);



        return 1;
    }
}
